package com.jixiang.tracedata.util;

import com.jixiang.tracedata.common.EventName;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author quanbiao
 * @date 2018-09-05
 */
public class StringOperate {

    /**
     * list对象拼接成字符串
     * @param objectList
     * @return
     */
    public static String listObjectToString(List<?> objectList){
        StringBuffer stringBuffer = new StringBuffer();
        for (Object  obj: objectList) {
            stringBuffer.append(obj.toString());
            stringBuffer.append(",");
        }
        return stringBuffer.toString().substring(0, stringBuffer.toString().length()-1);
    }

    /**
     * list string拼接成字符串
     * @param stringList
     * @param key
     * @return
     */
    public static String listStringToString(List<String> stringList, String key){
        StringBuffer stringBuffer = new StringBuffer();
        for (String  str: stringList) {
            stringBuffer.append("{" + key + ":");
            stringBuffer.append(str);
            stringBuffer.append("},");
        }
        return stringBuffer.toString().substring(0, stringBuffer.toString().length()-1);
    }

    /**
     * 从多行string里面取xml的信息
     * @param resourceStr
     * @return
     */
    public static String getXmlForMultiLine(String resourceStr){
        BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(resourceStr.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
        String line;
        StringBuffer strbuf=new StringBuffer();
        try {
            while ( (line = br.readLine()) != null ) {
                if(!line.trim().equals("")){
                    if(line.startsWith("<Msg>")){
                        return line;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 根据xml内容判断是否旅客事件还是航班时间
     * @param xmlContent
     * @return
     */
    public static String eventAffiliation(String xmlContent){

        if(xmlContent.contains(EventName.FLIGHT_DepTimeChg) ||
                xmlContent.contains(EventName.FLIGHT_EquipmentChg) ||
                xmlContent.contains(EventName.FLIGHT_GateChange) ||
                xmlContent.contains(EventName.FLIGHT_Hold) ||
                xmlContent.contains(EventName.FLIGHT_HoldRemove) ||
                xmlContent.contains(EventName.FLIGHT_InitFlight) ||
                xmlContent.contains(EventName.FLIGHT_StatusChange)
                ){
            return EventName.FLIGHT_EVENT;
        }else if(xmlContent.contains(EventName.TRAVELLER_ActionCodeChange) ||
                xmlContent.contains(EventName.TRAVELLER_AsrChange) ||
                xmlContent.contains(EventName.TRAVELLER_BagChange) ||
                xmlContent.contains(EventName.TRAVELLER_Boarded) ||
                xmlContent.contains(EventName.TRAVELLER_Book) ||
                xmlContent.contains(EventName.TRAVELLER_Cancel) ||
                xmlContent.contains(EventName.TRAVELLER_CheckIn) ||
                xmlContent.contains(EventName.TRAVELLER_CheckOut) ||
                xmlContent.contains(EventName.TRAVELLER_DeSuspend) ||
                xmlContent.contains(EventName.TRAVELLER_Downgrade) ||
                xmlContent.contains(EventName.TRAVELLER_EMDBoard) ||
                xmlContent.contains(EventName.TRAVELLER_EMDCheckIn) ||
                xmlContent.contains(EventName.TRAVELLER_EMDFlown) ||
                xmlContent.contains(EventName.TRAVELLER_EMDIssue) ||
                xmlContent.contains(EventName.TRAVELLER_EMDRefund) ||
                xmlContent.contains(EventName.TRAVELLER_EMDVOID) ||
                xmlContent.contains(EventName.TRAVELLER_Exchange) ||
                xmlContent.contains(EventName.TRAVELLER_Flown) ||
                xmlContent.contains(EventName.TRAVELLER_IDChange) ||
                xmlContent.contains(EventName.TRAVELLER_Issue) ||
                xmlContent.contains(EventName.TRAVELLER_NameChange) ||
                xmlContent.contains(EventName.TRAVELLER_Noshow) ||
                xmlContent.contains(EventName.TRAVELLER_OSIChange) ||
                xmlContent.contains(EventName.TRAVELLER_PassengerProtection) ||
                xmlContent.contains(EventName.TRAVELLER_ReprintBoardingCard) ||
                xmlContent.contains(EventName.TRAVELLER_Revalidation) ||
                xmlContent.contains(EventName.TRAVELLER_RMKChange) ||
                xmlContent.contains(EventName.TRAVELLER_SeatChange) ||
                xmlContent.contains(EventName.TRAVELLER_Split) ||
                xmlContent.contains(EventName.TRAVELLER_SSRChange) ||
                xmlContent.contains(EventName.TRAVELLER_Standby) ||
                xmlContent.contains(EventName.TRAVELLER_Suspend) ||
                xmlContent.contains(EventName.TRAVELLER_TicketCheckIn) ||
                xmlContent.contains(EventName.TRAVELLER_TRFD) ||
                xmlContent.contains(EventName.TRAVELLER_Upgrade) ||
                xmlContent.contains(EventName.TRAVELLER_VT)
                ){
            return EventName.TRAVELLER_EVENT;
        }
        return "";
    }
}
